package com.myy.auth.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.myy.auth.entity.System;
import com.myy.auth.mapper.SystemMapper;
import com.myy.auth.vo.DictVo;
import com.myy.auth.vo.SystemSearchVo;
import com.myy.auth.vo.SystemVo;
import com.myy.common.exception.ParamException;
import com.myy.common.utils.ValueUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class SystemService extends ServiceImpl<SystemMapper, System> {

    /**
     * 分页列表·
     * @param searchVo
     * @return
     */
    public IPage<SystemVo> selectPage(SystemSearchVo searchVo) {
        IPage<System> ipage = this.getBaseMapper().selectPage(searchVo, new LambdaQueryWrapper<System>()
                .eq(StrUtil.isNotBlank(searchVo.getSysCode()), System:: getSysCode, searchVo.getSysCode())
                .eq(StrUtil.isNotBlank(searchVo.getSysName()), System:: getSysName, searchVo.getSysName()));
        List<SystemVo> voList = ipage.getRecords()
                .stream()
                .map(e -> {
                    SystemVo vo = BeanUtil.toBean(e, SystemVo.class);
                    return vo;
                })
                .collect(Collectors.toList());
        IPage<SystemVo> voIpage = new Page<SystemVo>();
        voIpage.setRecords(voList);
        voIpage.setSize(ipage.getSize());
        voIpage.setCurrent(ipage.getCurrent());
        voIpage.setTotal(ipage.getTotal());
        return voIpage;
    }

    /**
     * 系统创建
     */
    public String saveOrUpdate(SystemVo vo) {
        valib(vo);
        System po = null;
        if(StrUtil.isNotBlank(vo.getId())){
            po = getById(vo.getId());
            if(po == null){
                throw new ParamException("无效的ID");
            }
        }
        if(po == null){
            po = BeanUtil.copyProperties(vo, System.class);
            po.setId(IdUtil.objectId());
            po.setStatus("1");
            po.init();
            save(po);
            return "创建成功";
        }else{
            BeanUtil.copyProperties(vo,po);
            po.initByUpdate();
            updateById(po);
            return "更新成功";
        }
    }
    //有效参数
    private void valib(SystemVo vo){
        Assert.notNull(vo,"参数不能空");
//        List<SysSystem> sysSystemList = this.getBaseMapper().selectList(new LambdaQueryWrapper<SysSystem>()
//                .eq(SysSystem::getSysCode, vo.getSysCode()));
        Boolean exists = this.lambdaQuery()
                .eq(System::getSysCode, vo.getSysCode())
                .exists();
        Assert.isTrue(exists,"系统编码已存在");
    }

    /**
     * 系统列表
     */
    public List<DictVo> selectList(String name) {
        return this.getBaseMapper().findSysListByName(ValueUtil.like(name));
    }
}
